class Post < ActiveRecord::Base
  has_many :comments, :dependent => :destroy

  has_many :top_comments, :class_name => 'Comment', :conditions => {:comment_id => nil}

  def self.date_list
    first_post = Post.find :first
    return [] unless first_post
    years = Time.now.year - first_post.created_at.year
    if years.zero?
      sum_months = (Time.now.month - first_post.created_at.month + 1)
      return Post.generate_date_list(first_post.created_at, sum_months)
    end

    sum_months = years*12 - first_post.created_at.month + 1 + Time.now.month
    return Post.generate_date_list(first_post.created_at, sum_months)
  end

  private
  def self.generate_date_list(begin_date, sum_months)
    date_list = []

    for i in (1..sum_months)
      month = begin_date.month
      month = "0" + month.to_s if month < 10
      date_list << "#{begin_date.year}-#{month}"
      begin_date = 1.month.since(begin_date)
    end

    date_list.reverse!
  end

end
